package com.imesne.office.excel.model;

import com.imesne.office.excel.exception.OfficeException;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by liyd on 17/6/30.
 */
public class ExcelSheet extends ExcelProperties {

    private Sheet sheet;

    private Integer sheetNum;

    /**
     * sheet名称
     */
    private String sheetName;

    /**
     * 标题
     */
    private ExcelRow titleRow;

    /**
     * 行数据
     */
    private Map<Integer, ExcelRow> excelRows;

    /**
     * 最后的行号 跟excelRows.size()不一定对应，有可能会有空行等其它情况
     */
    private int lastRowNum;

    public ExcelSheet(String sheetName) {
        this.sheetName = sheetName;
        excelRows = new LinkedHashMap<>();
    }

    public ExcelSheet() {
        this(null);
    }

    public ExcelRow getRow(int rowNum) {
        return this.excelRows.get(rowNum);
    }

    public void addRow(ExcelRow excelRow) {
        if (excelRow.getRowNum() == null) {
            throw new OfficeException("行号不能为空");
        }
        excelRows.put(excelRow.getRowNum(), excelRow);
        if (lastRowNum < excelRow.getRowNum()) {
            lastRowNum = excelRow.getRowNum();
        }
    }

    public ExcelRow createTitleRow() {
        return this.createTitleRow(1);
    }

    public ExcelRow createTitleRow(int rowNum) {
        this.titleRow = new ExcelRow();
        titleRow.setRowNum(rowNum);
        return this.titleRow;
    }

    public ExcelRow createRow(int rowNum) {
        ExcelRow excelRow = new ExcelRow();
        excelRow.setRowNum(rowNum);
        this.addRow(excelRow);
        return excelRow;
    }

    @Override
    public String toString() {

        String lineSeparator = System.lineSeparator();
        StringBuilder sb = new StringBuilder();
        sb.append(sheetName);
        sb.append(lineSeparator);

        if (this.titleRow != null) {
            sb.append("\t")
                    .append("title:{")
                    .append(lineSeparator)
                    .append("\t\t")
                    .append(this.titleRow)
                    .append(lineSeparator)
                    .append("\t}")
                    .append(lineSeparator);
        }

        sb.append("\t").append("rows:{").append(lineSeparator);
        for (Map.Entry<Integer, ExcelRow> entry : excelRows.entrySet()) {
            sb.append("\t\t")
//                    .append(entry.getKey())
//                    .append(" > ")
                    .append(entry.getValue())
                    .append(lineSeparator);
        }
        sb.append("\t}");
        return sb.toString();
    }

    public Collection<ExcelRow> getExcelRows() {
        return excelRows.values();
    }

    public Sheet getSheet() {
        return sheet;
    }

    public void setSheet(Sheet sheet) {
        this.sheet = sheet;
    }

    public Integer getSheetNum() {
        return sheetNum;
    }

    public void setSheetNum(Integer sheetNum) {
        this.sheetNum = sheetNum;
    }

    public ExcelRow getTitleRow() {
        return titleRow;
    }

    public void setTitleRow(ExcelRow titleRow) {
        this.titleRow = titleRow;
    }

    public String getSheetName() {
        return sheetName;
    }

    public void setSheetName(String sheetName) {
        this.sheetName = sheetName;
    }

    public void setLastRowNum(int lastRowNum) {
        this.lastRowNum = lastRowNum;
    }

    public int getLastRowNum() {
        return lastRowNum;
    }
}
